package club.beimeng.emailserver2.utils;


import club.beimeng.emailserver2.entity.EmailAccount;
import lombok.Data;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.logging.Logger;

@Data
@Order(1)
@Component
@ConfigurationProperties(prefix = "club.beimeng.email")
public class ApplicationProperties implements CommandLineRunner {

    public List<EmailAccount> accounts; // 加载账号列表

    public int timeDelay; // 单个账号最短发送冷却时间

    public int threads;// 配置最多的线程数量

    public String signature; // 发件人签名

    /*定义一些公开的静态的常量*/
    public static int TIME_DELAY;;
    public static List<EmailAccount> ACCOUNTS;
    public static int THREADS = 10; //线程池大小 默认为10
    public static String SIGNATURE = "软件协会";

    private Logger logger;

    @Override
    public void run(String... args) throws Exception {
        logger = Logger.getLogger("加载配置文件 - ApplicationProperties");
        logger.info("开始加载配置文件");

        if (timeDelay != 0){
            TIME_DELAY = timeDelay;
            logger.info("TIME_DELAY:" + TIME_DELAY );
        }
        if (accounts != null){
            ACCOUNTS = accounts;
            logger.info("ACCOUNTS:" + ACCOUNTS );
        }
        if (threads != 0){
            THREADS = threads;
            logger.info("THREADS:" + THREADS + "[默认线程池大小=10]");
        }
        if (signature != null){
            SIGNATURE = signature;
            logger.info("SIGNATURE:" + SIGNATURE + "[默认签名=软件协会]");
        }
    }
}
